/************************************************************************************
* HistoryManager.cpp                                                                *
*                                                                                   *
* Copyright (C) 2011 Varuna L Amachi                                                *
*                                                                                   *
* This program is free software; you can redistribute it and/or modify              *
* it under the terms of the GNU General Public License as published by              *
* the Free Software Foundation; either version 2 of the License, or any             *
* later version.                                                                    *
*                                                                                   *
* This program is distributed in the hope that it will be useful, but               *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY        *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for       *
* more details. You should have received a copy of the GNU General Public           *
* License along with this program; if not, write to the Free Software Foundation,   *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                       *
*                                                                                   *
*************************************************************************************/

#include "HistoryManager.h"
#include "interfaces/Playlist.h"
#include "HistoryModel.h"

#include <QVBoxLayout>
#include <QAction>

using namespace Yamp;

HistoryManager::HistoryManager(YContext *context, QWidget *parent) :
YampComponent(context, parent) {
    m_context->player()->addListener(this);
    m_refreshRequired = false;
    createView();
    setupUi();
    setupActions();
}


void HistoryManager::createView() {
    QList<QString> *trackIds = m_context->ydb()->retriever()->retrieveHistory();

    QList<CollectorItem *>* items = new QList<CollectorItem *>();
    m_context->collector()->items(*trackIds, *items);
    HistoryModel *model = new HistoryModel(m_context, HistoryModel::RecentlyPlayed, this);
    SimpleCollectionSortFilter *filter = new SimpleCollectionSortFilter(
                m_context, model);
    model->setModel(items);
    m_view = new SimpleCollectionView(model, filter, this);
    delete trackIds;
}

void HistoryManager::setupUi() {
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(m_view);
    this->setLayout(layout);
}


void HistoryManager::setupActions() {
    QList<QAction *>* actions = new QList<QAction *>();
    m_clearAction = new QAction(tr("Clear"), m_view);
    m_trackAddAction  = new QAction(tr("Add to playlist"), m_view);
    (*actions) << m_clearAction << m_trackAddAction;
    connect(m_view, SIGNAL(itemActivated(CollectorItem*)), this, SLOT(itemActivated(CollectorItem*)));
    connect(m_trackAddAction, SIGNAL(triggered()), this, SLOT(handlePlayAction()));
    connect(m_clearAction, SIGNAL(triggered()), m_view->model(), SLOT(clear()));
    connect(m_clearAction, SIGNAL(triggered()), this, SLOT(handleClearAction()));
    connect(m_view, SIGNAL(contextMenuRequested()), this, SLOT(preContextMenuProcess()));
    connect(m_context->collector(), SIGNAL(aboutToRemoveItems(QList<QString>*)),
            this, SLOT(itemsRemovedFromCollection(QList<QString> *)));
    m_view->setContextActions(actions);
}


void HistoryManager::itemActivated(CollectorItem *item) {
    m_context->playlist()->append(item, true);
}


void HistoryManager::handlePlayAction() {
    QList<QString> *itemIds = m_view->selectedTrackIds();
    QList<CollectorItem *> *items = new QList<CollectorItem *>();
    m_context->collector()->items(*itemIds, *items);
    QList<MediaItem *> *medias = new QList<MediaItem *> ();
    for(int i = 0; i < items->size(); ++i) {
        medias->append(items->at(i));
    }
    m_context->playlist()->append(medias);
    delete itemIds;
    delete items;

}


void HistoryManager::playStarted(MediaItem *item) {
    if(item->itemType() == CollectorItem::ITEM_TYPE) {
        m_refreshRequired = true;
        if(isVisible()) {
            updateView();
        }
    }
}



void HistoryManager::showEvent(QShowEvent *event) {
    if(m_refreshRequired) {
        updateView();
    }
    QWidget::showEvent(event);
}

void HistoryManager::preContextMenuProcess() {
    QList<QString> *itemIds = m_view->selectedTrackIds();
    bool anySelected = itemIds->size() != 0;
    m_trackAddAction->setEnabled(anySelected);
    delete itemIds;
    m_clearAction->setEnabled(! m_view->model()->isEmpty());
}

QString HistoryManager::name() {
    return tr("Recently Played");
}

void HistoryManager::handleClearAction() {
    m_context->ydb()->updater()->clearHistory();
}


void HistoryManager::itemsRemovedFromCollection(QList<QString> *ids) {
    m_context->ydb()->updater()->removeHistoryItems(*ids);
    QList<QString> *trackIds = m_context->ydb()->retriever()->retrieveHistory();
    QList<CollectorItem *>* items = new QList<CollectorItem *>();
    m_context->collector()->items(*trackIds, *items);
    m_view->model()->setModel(items);
    delete trackIds;
}


void HistoryManager::updateView() {
    QList<QString> *ids = m_context->ydb()->retriever()->retrieveHistory();
    QList<CollectorItem *>* items = new QList<CollectorItem *>();
    m_context->collector()->items(*ids, *items);
    m_view->model()->setModel(items);
    m_view->sort(4, Qt::DescendingOrder);
    delete ids;
    m_refreshRequired =false;
}
